Skip to content

added Enable/Disable WiFi functionality #4670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open

added Enable/Disable WiFi functionality #4670

wants to merge 30 commits into from

Conversation

rhkean
Copy link

@rhkean rhkean commented May 1, 2025

  • Added ability to disable WiFi
  • enabling or disabling of WiFi takes effect after restart
  • AP can be temporarily started with a Button0 long press

Summary by CodeRabbit

  • New Features

    • Added the ability to fully enable or disable WiFi via a new control flag.
    • Improved handling for toggling Access Point (AP) and Station (STA) modes through updated logic in the WiFi settings.
  • Bug Fixes

    • Centralized and streamlined the shutdown process for the WiFi Access Point, improving reliability when disabling AP mode.

Copy link
Contributor

coderabbitai bot commented May 1, 2025

Walkthrough

This change introduces a new method, shutdownAP(), to centralize logic for disabling the WiFi access point (AP) and updates all relevant code paths to use it. It also adds a new wifiEnabled flag to WiFi options, incorporates logic for toggling WiFi enablement, and refactors JSON handling for WiFi state changes.

Changes

Files/Paths Change Summary
wled00/wled.cpp Added the shutdownAP() method to encapsulate AP shutdown logic. Replaced direct AP shutdown calls in initConnection() and handleConnection() with this method. Added a guard in handleConnection() to skip processing if wifiEnabled is false. Updated AP start condition to exclude AP_BEHAVIOR_BUTTON_ONLY mode.
wled00/wled.h Added wifiEnabled bitfield to WiFiOptions class and updated its constructor. Initialized wifiEnabled to true in the global wifiOpt instance. Introduced a macro and, for certain builds, a global variable for wifiEnabled. Declared the new public method shutdownAP() in the WLED class.
wled00/json.cpp Refactored WiFi JSON state handling in deserializeState(): replaced direct AP shutdown code with a call to shutdownAP(). Added logic to process the "on" key for toggling STA mode, updating wifiEnabled and forceReconnect accordingly, and disconnecting or shutting down AP as needed. Removed commented-out "restart" flag handling.

Suggested reviewers

  • blazoncek

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a3d805 and ffab0e2.

📒 Files selected for processing (3)
  • wled00/json.cpp (1 hunks)
  • wled00/wled.cpp (6 hunks)
  • wled00/wled.h (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • wled00/json.cpp
  • wled00/wled.cpp
  • wled00/wled.h
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
wled00/wled.cpp (1)

569-841: Consider adding explicit WiFi shutdown when disabled.

While the current implementation correctly prevents WiFi operations when WiFi is disabled, it might be beneficial to explicitly turn off the WiFi hardware when wifiEnabled is false to save power.

Currently, the code skips operations but doesn't actively shut down WiFi if it was previously enabled before the flag was changed.

You could add code like this near the beginning of handleConnection():

void WLED::handleConnection()
{
  static bool scanDone = true;
  static byte stacO = 0;
  const unsigned long now = millis();
  const unsigned long nowS = now/1000;
  const bool wifiConfigured = WLED_WIFI_CONFIGURED;

+ // Ensure WiFi is completely turned off when disabled
+ if (!wifiEnabled && (WiFi.getMode() != WIFI_OFF)) {
+   DEBUG_PRINTLN(F("WiFi disabled, turning off radio"));
+   WiFi.mode(WIFI_OFF);
+   return;
+ }

  // ignore connection handling if WiFi is configured and scan still running
  // or within first 2s if WiFi is not configured or AP is always active
  if ((wifiConfigured && multiWiFi.size() > 1 && WiFi.scanComplete() < 0) || (now < 2000 && (!wifiConfigured || apBehavior == AP_BEHAVIOR_ALWAYS)))
    return;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d10714d and 5737cdb.

📒 Files selected for processing (13)
  • package.json (1 hunks)
  • wled00/button.cpp (1 hunks)
  • wled00/cfg.cpp (2 hunks)
  • wled00/const.h (1 hunks)
  • wled00/data/settings_wifi.htm (2 hunks)
  • wled00/fcn_declare.h (3 hunks)
  • wled00/json.cpp (2 hunks)
  • wled00/network.cpp (1 hunks)
  • wled00/set.cpp (1 hunks)
  • wled00/um_manager.cpp (1 hunks)
  • wled00/wled.cpp (8 hunks)
  • wled00/wled.h (2 hunks)
  • wled00/xml.cpp (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
wled00/um_manager.cpp (2)
wled00/fcn_declare.h (4)
  • getUMData (454-454)
  • data (419-419)
  • data (419-419)
  • isModEnabled (474-474)
usermods/audioreactive/audio_reactive.cpp (2)
  • data (1438-1443)
  • data (1438-1438)
wled00/wled.cpp (3)
wled00/wled.h (1)
  • resetAP (1068-1068)
wled00/util.cpp (2)
  • prepareHostname (118-139)
  • prepareHostname (118-118)
wled00/udp.cpp (4)
  • espNowSentCB (945-947)
  • espNowSentCB (945-945)
  • espNowReceiveCB (950-1033)
  • espNowReceiveCB (950-950)
wled00/fcn_declare.h (1)
usermods/mpu6050_imu/usermod_gyro_surge.h (1)
  • um_data (163-197)
🔇 Additional comments (34)
package.json (2)

28-29: Reordering dependencies in package.json.

The order of dependencies in package.json doesn't affect functionality. This change simply reorders "nodemon" and "web-resource-inliner" alphabetically.


34-35: Added trailing newline.

Adding a trailing newline at the end of JSON files is a standard best practice.

wled00/const.h (1)

213-213: Added new usermod ID for WiFi enable/disable feature.

The new usermod ID is properly defined with a sequential ID (58) and references the implementation file "disable_wifi.h". This aligns with the PR objective of adding WiFi enable/disable functionality.

wled00/set.cpp (1)

23-23: Added WiFi enablement flag processing.

This line captures the WiFi enabled state from the HTTP request parameter "EN" when handling WiFi settings updates. It follows the same pattern used for other WiFi settings in this function.

wled00/xml.cpp (2)

201-201: Added WiFi enabled checkbox to settings form.

Properly adds the checkbox for enabling/disabling WiFi to the settings page, using the same pattern as other settings.


202-206: Added dynamic UI control for WiFi-related elements.

These lines dynamically enable or disable WiFi-related UI controls based on the WiFi enabled status. This provides appropriate UI feedback when WiFi is disabled. The implementation targets:

  1. The scan button
  2. SSID input field
  3. Password input field
  4. BSSID input field

This ensures users can't attempt to configure WiFi settings when the feature is disabled.

wled00/cfg.cpp (2)

100-100: WiFi enabled flag integration in config deserialization

The new code correctly adds support for loading the wifiEnabled flag from configuration JSON. The bitwise OR operation (|) preserves the current value if the key is missing in the JSON.


769-769: WiFi enabled flag integration in config serialization

This change properly adds the wifiEnabled flag to the WiFi configuration JSON object during serialization, completing the configuration persistence feature.

wled00/network.cpp (1)

322-322: Properly integrated WiFi enabled check in configuration verification

The modification correctly adds the WiFi enabled state as a prerequisite for considering WiFi configured. This ensures WiFi operations won't be attempted when disabled.

wled00/data/settings_wifi.htm (2)

159-162: Added WiFi enable/disable UI control with restart notice

Good addition of the WiFi enable checkbox with a clear message that the change requires a device restart.


108-116: Well-implemented UI state management for WiFi controls

The function effectively toggles the disabled state of relevant WiFi configuration controls based on the checkbox state. This is a good UX improvement that prevents users from configuring WiFi while it's disabled.

wled00/um_manager.cpp (3)

34-34: Expanded usermod ID range by changing parameter type

Changing uint8_t to uint16_t for mod_id expands the range of supported usermod IDs from 256 to 65536, providing room for future growth.


36-36: Consistency improvement for mod_id comparison

Updated comparison logic to match the wider uint16_t type. This change maintains consistency with the parameter type change.


41-47: New method to check if a usermod is enabled

This method properly implements checking if a specified usermod is enabled, which is essential for the WiFi enable/disable functionality. The implementation correctly returns the usermod's enabled state when found.

However, there's a potential issue: if the requested usermod is not found, the function returns false. Consider adding a comment to clarify this behavior or returning a more explicit result like -1 to distinguish between "not found" and "found but disabled".

wled00/json.cpp (2)

480-486: Thorough implementation of WiFi-dependent AP mode logic.

The change correctly integrates the new wifiEnabled flag with AP mode activation logic. AP mode will only be started if currently inactive, explicitly requested, and WiFi is enabled. Similarly, AP mode will be stopped if it's active but either WiFi is disabled or AP mode is no longer requested.


727-727: Good addition of WiFi enabled state to JSON information.

Adding the WiFi enabled state to the serialized info JSON output is appropriate and necessary for clients to properly reflect the current WiFi state.

wled00/fcn_declare.h (3)

433-433: Good default implementation for isEnabled() method.

Adding this virtual method to the Usermod base class provides a clean extension point for implementing enable/disable functionality in usermod classes. The default implementation returning true ensures backward compatibility with existing usermods.


454-454: Parameter type change for usermod ID.

Changing the mod_id parameter from uint8_t to uint16_t allows for a larger range of usermod IDs. The comment correctly explains that USERMOD_ID_RESERVED will poll all usermods.


474-474: New function to check if a usermod is enabled.

This function provides a way to check if a specific usermod is enabled, which is essential for the new WiFi enable/disable functionality.

wled00/wled.h (5)

348-348: Good addition of WiFi enable flag to WiFiOptions structure.

The 1-bit flag for WiFi enabled state is memory-efficient and properly integrated into the WiFiOptions structure.


350-358: Updated constructor to initialize new WiFi enabled flag.

The constructor properly initializes the new wifiEnabled flag.


361-363: Consistent initialization of wifiEnabled to true by default.

The flag is initialized to true for both ESP32 and other architectures, which is a good backward-compatible default.


371-371: Added access macro for the WiFi enabled flag.

This macro provides a consistent way to access the WiFi enabled flag regardless of the underlying implementation.


383-383: Added global wifiEnabled variable for non-RAM-saving configurations.

This complements the packed structure approach for configurations without WLED_SAVE_RAM.

wled00/button.cpp (2)

325-346: Changed button handling logic for button 0.

The restructured code now has a clearer logic flow with nested if-else blocks:

  1. Long press > WLED_LONG_AP (5 seconds):

    • If press > WLED_LONG_FACTORY_RESET (10 seconds): performs factory reset
    • Otherwise: starts AP mode with initAP(true)
  2. Short press ≤ WLED_LONG_AP: sets doReboot = true

The major change here is that short presses now trigger a reboot rather than the previous behavior. This seems intentional as it fits with the PR description of long press to start AP mode.

Please confirm that the change in behavior for short press on button 0 (now triggering a reboot) is intentional.


334-341: Commented usermod code for WiFi management.

There's commented code that seems to be related to enabling WiFi through a usermod. Since it's commented out, it has no functional impact, but it provides useful context about potential future integration with a dedicated WiFi management usermod.

wled00/wled.cpp (8)

569-570: Good implementation of WiFi enablement check in initAP().

The code properly prevents Access Point initialization when WiFi is disabled or when AP behavior is set to button-only mode, which aligns with the PR's objective to control WiFi functionality.


572-574: Enhanced debugging for AP behavior.

Adding debug output for the current AP behavior setting is helpful for troubleshooting WiFi connectivity issues.


637-639: Well-placed early return for disabled WiFi.

This early return properly prevents all WiFi connection attempts when WiFi is disabled, ensuring the device respects the user's preference to disable WiFi functionality.


674-691: Properly gated ESP-NOW initialization.

The ESP-NOW initialization is now correctly only performed when WiFi is enabled, while maintaining all the original functionality. The indentation changes make the code more readable.


765-765: Correct WiFi enablement check in handleConnection().

Adding the WiFi enabled condition here prevents unnecessary reconnection attempts when WiFi is deliberately disabled.


796-796: Properly gated disconnection handling.

This check ensures the system doesn't try to handle WiFi disconnections when WiFi is intentionally disabled, which is the correct behavior.


823-825: Improved debug information for AP startup.

The enhanced debug message now includes the AP behavior setting, which is helpful for troubleshooting AP initialization issues.


841-841: Properly conditioned improv response handling.

The code correctly prevents sending improv responses when WiFi is disabled, ensuring consistent behavior when WiFi functionality is turned off.

Copy link
Collaborator

@DedeHai DedeHai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I briefly looked at the code, generally looks ok. did not test if it works as intended.

@rhkean
Copy link
Author

rhkean commented May 1, 2025

One thing that I forgot to note...

I do not have an Ethernet board to test the USER_ETHERNET features. I think that I covered that area correctly, however, if someone with a board could test that Ethernet still works correct, I would greatly appreciate it.

@rhkean
Copy link
Author

rhkean commented May 1, 2025

I'd also be interested in others' thoughts regarding the "turn WiFi off" comments from code rabbit.

@rhkean rhkean requested a review from DedeHai May 1, 2025 23:44
Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I'll have to say no to this PR.

First, if you disable WiFi, how do you enable it again? Even AP mode on button press is disabled so there will be no way to enable it again. Or did I miss such behaviour?

Second, even though ESP-NOW uses WiFi circuitry it is not a WiFi protocol and should not be disabled. There is an option for enabling/disabling ESP-NOW in the settings.

Thirdly, there are unnecessary changes.

@rhkean
Copy link
Author

rhkean commented May 2, 2025

I'm afraid I'll have to say no to this PR.

First, if you disable WiFi, how do you enable it again? Even AP mode on button press is disabled so there will be no way to enable it again. Or did I miss such behaviour?

the AP still starts with a button0 longpress (see 1st line in initAP().

Second, even though ESP-NOW uses WiFi circuitry it is not a WiFi protocol and should not be disabled. There is an option for enabling/disabling ESP-NOW in the settings.

I did not realize that ESP-NOW was not a WiFi protocol. I'll remove those code changes. That was my mistake.

Thirdly, there are unnecessary changes.
Let me make the changes requested by you and @DedeHai and I'll pester you some more with another review. If there are additional unnecessary changes that are not noted so far, please let me know and I'll clean those up too.

@DedeHai
Copy link
Collaborator

DedeHai commented May 2, 2025

ESPNow and wifi hardware are tightly entangled, it is not possible to disable wifi and have ESPNow enabled, at least in arduino framework afaik.
Also the ability to disable wifi should be a usermod or at least a setting that first needs to be activated , hidden by default, with a big warning. Users tend to go nuts with random settings and I see much potential for bricked setups

@rhkean
Copy link
Author

rhkean commented May 2, 2025

ESPNow and wifi hardware are tightly entangled, it is not possible to disable wifi and have ESPNow enabled, at least in arduino framework afaik.
so I should ignore coderabbit's suggestion about turning off the hardware. :)

Also the ability to disable wifi should be a usermod or at least a setting that first needs to be activated , hidden by default, with a big warning. Users tend to go nuts with random settings and I see much potential for bricked setups

The setting initializes to Active, can be enabled via b0 longpress, ETH (if installed), the BLE usermod (once I finish it)
Not sure what suggestion or preference you have with respect to making the option hidden.

OK, @blazoncek said in an earlier PR of mine that he felt that disable WiFi should be integrated, not a usermod. So, I'm not sure which direction to go there... I already ripped out the usermod code.

@rhkean
Copy link
Author

rhkean commented May 2, 2025

I've made the code changes noted above. But, I still have to test the rebuild. I'll do that tonight and refresh the PR.

@rhkean
Copy link
Author

rhkean commented May 8, 2025

Added JSON API
added wifi["on"] and wifi["pwrOff"]

  • button0 longpress will start the AP
  • {"wifi":{"on":true}} will connect to configured wifi
  • {"wifi":{"on":false}} will disconnect wifi leaving circuitry on
  • {"wifi":{"on":false},"pwrOff":false} will disconnect wifi leaving circuitry on
  • {"wifi":{"on":false},"pwrOff":true} will disconnect wifi and turn off the circuitry

@rhkean rhkean requested a review from blazoncek May 8, 2025 02:20
@rhkean
Copy link
Author

rhkean commented May 9, 2025

Added JSON API added wifi["on"] and wifi["pwrOff"]

  • button0 longpress will start the AP
  • {"wifi":{"on":true}} will connect to configured wifi
  • {"wifi":{"on":false}} will disconnect wifi leaving circuitry on
  • {"wifi":{"on":false},"pwrOff":false} will disconnect wifi leaving circuitry on
  • {"wifi":{"on":false},"pwrOff":true} will disconnect wifi and turn off the circuitry

I'm trying to track down the cause, but I've found, what I think is, an issue.
if I create a preset {"wifi":{"on":false,"pwrOff":true}} and set it to "Apply on boot", it works perfectly if wifiConfigured = false

if, however, wifiConfigured = true, then it connects to the WiFi, then gets disabled, but does not release the IP... watching the debug output, it continues to report the leased IP instead of 0.0.0.0. It also, seems to be taking more than 1 execution of the toggle preset to get it to reconnect to the network. (the multiple toggling to get it to re-enable doesn't happen if I don't select pwrOff, but the IP issue does)

(side note, it appears that saving a preset with "apply at boot" causes that preset to execute immediately. is that a bug?)

@rhkean
Copy link
Author

rhkean commented May 9, 2025

I'm trying to track down the cause, but I've found, what I think is, an issue. if I create a preset {"wifi":{"on":false,"pwrOff":true}} and set it to "Apply on boot", it works perfectly if wifiConfigured = false

if, however, wifiConfigured = true, then it connects to the WiFi, then gets disabled, but does not release the IP... watching the debug output, it continues to report the leased IP instead of 0.0.0.0. It also, seems to be taking more than 1 execution of the toggle preset to get it to reconnect to the network. (the multiple toggling to get it to re-enable doesn't happen if I don't select pwrOff, but the IP issue does)

figured it out...
WiFi init is started before the bootPresets are applied, but before the WiFi connects...
so a proper WiFi.disconnect() is not getting called.

@blazoncek
Copy link
Collaborator

Does it need to release IP? It will get a new one on reconnect if needed.
If that interferes with your processing use WiFi.status() and check if it is either WL_IDLE_STATUS or WL_DISCONNECTED (or !WL_CONNECTED).

As for boot preset: It is executed in async fashion (as are all presets).

@rhkean
Copy link
Author

rhkean commented May 9, 2025

Does it need to release IP? It will get a new one on reconnect if needed. If that interferes with your processing use WiFi.status() and check if it is either WL_IDLE_STATUS or WL_DISCONNECTED (or !WL_CONNECTED).

I, personally, don't care as long as the radio is off. It feels like bad practice, though... I'm not sure. I'll spit out some more debug statements and do some more testing after work. I'll pull in my BLE code and verify it doesn't crash. LOL

As for boot preset: It is executed in async fashion (as are all presets).

yeah, I figured. as is the WiFi connect call, I'm seeing. Hence the timing of it all. I'll try to get it to behave with a little more consistency.

@blazoncek
Copy link
Collaborator

Async behaviour is intended. It should be implemented everywhere.

@rhkean
Copy link
Author

rhkean commented May 10, 2025

I verified that the connection status is reporting as connect in the situation discussed earlier... I've updated the code to handle it.

@netmindz
Copy link
Member

It maybe a stupid question, but why do you want to disable WIFI?
I think each new feature should have a clear use case, but this PR does not give any indication of why you would want this feature

@rhkean
Copy link
Author

rhkean commented May 10, 2025

additional testing showed that AP was not being enabled/disabled properly.... fixed now

@rhkean
Copy link
Author

rhkean commented May 10, 2025

It maybe a stupid question, but why do you want to disable WIFI? I think each new feature should have a clear use case, but this PR does not give any indication of why you would want this feature

it's actually mentioned in many of the comments of this PR.

  • power saving (especially helpful for battery power)
  • my other mod (in progress) is BLE. I'm hoping to get COEXISTENCE to work, but that will probably end up being a 2.0 version of the mod, bc it's not cooperating, so I'm focusing on single use antennae config initially

@rhkean
Copy link
Author

rhkean commented May 17, 2025

any decisions on this @blazoncek ? @DedeHai ?

@DedeHai
Copy link
Collaborator

DedeHai commented May 17, 2025

can you explain the current state?
why are there two parameters, enable and power?
from what I see in code they are redundant or at least unclear. if the goal is to disable wifi, one parameter is enough and disable should be fully disable, including AP and ESPnow, anything else is just a bit convoluted. Or can you elaborate the intention and use cases?

@blazoncek
Copy link
Collaborator

any decisions on this @blazoncek ?

I know this may work for you but I am not comfortable with the implementation as it is based on a workaround.

I am not sure why would you want to circumvent initial WiFi connect as it has no side effects and will run at most for about 10 or so seconds.

What I particularly don't like is disconnecting WiFi in event handling. IMO it is not the correct place to do it.

Unfortunately I'm lacking in time ATM for testing but have a slightly different approach in my dev branch. Once I test it I'll let you know if it is any better.

@rhkean
Copy link
Author

rhkean commented May 17, 2025

can you explain the current state? why are there two parameters, enable and power? from what I see in code they are redundant or at least unclear. if the goal is to disable wifi, one parameter is enough and disable should be fully disable, including AP and ESPnow, anything else is just a bit convoluted. Or can you elaborate the intention and use cases?

great question
as noted in one of @blazoncek 's messages earlier, the call to WiFi.disconnect(false) will disconnect WiFI, however WiFi.disconnect(true) will power off the WiFi circuitry for better power saving. So, I added the extra option for both as a "bonus" feature.

for me, personally, I don't want to turn off the circuitry, bc I'm trying to use it for BLE, so I saw the benefit for both options.

@rhkean
Copy link
Author

rhkean commented May 17, 2025

any decisions on this @blazoncek ?

I know this may work for you but I am not comfortable with the implementation as it is based on a workaround.

I am not sure why would you want to circumvent initial WiFi connect as it has no side effects and will run at most for about 10 or so seconds.

My testing showed that the actual timing was not actually turning off the WiFi if it is configured when using a preset to run on boot. I placed a gazillion log statements in so that I could trace it out and it played out in this order:

  1. boot
  2. init wifi and check if WiFi is configured
  3. start scan
  4. request boot presets
  5. preset runs
  6. scan completes and connects

if there is a better way to have the preset run on boot, but delay until after WiFi connects, that would be perfect I think

I thought about adding a static disableWiFi(bool pwrOff) method to the WLED class that could be called from a usermod if needed (such as the BLE mod I'm working on)

What I particularly don't like is disconnecting WiFi in event handling. IMO it is not the correct place to do it.

agreed. I wasn't too happy about that, either. however, my BLE startup kept crashing. :)

Unfortunately I'm lacking in time ATM for testing but have a slightly different approach in my dev branch. Once I test it I'll let you know if it is any better.

I haven't had a chance to look at your fork yet. I try to get to that this weekend. If you have reworked the WiFi startup though, it'll probably work a lot smoother. Please keep me in the loop.

I really would love for my contributions to be of value and not just a "well it works for me so I'll just run my own builds" scenario. So, I greatly appreciate all the feedback.

@blazoncek
Copy link
Collaborator

@rhkean what I meant with "why would you want to circumvent initial WiFi connect" is: let it connect and turn off WiFi after about 10 or so seconds using a playlist with two preset - one dummy that does nothing but runs for 10 (or so) seconds and another one that turns off WiFi.

@rhkean
Copy link
Author

rhkean commented May 18, 2025

@rhkean what I meant with "why would you want to circumvent initial WiFi connect" is: let it connect and turn off WiFi after about 10 or so seconds using a playlist with two preset - one dummy that does nothing but runs for 10 (or so) seconds and another one that turns off WiFi.

well, obviously, I didn't think of that. ROFL

here's the simplified implementation. Only a wifiEnabled bool flag to test, and a JSON API call.

I noticed that the AP shutdown code was implemented 6 different times, so I consolidated it into a single WLED method.

@xxv
Copy link
Contributor

xxv commented May 18, 2025

Hey folks. I just wanted to chime in here as a user who would love to turn off wifi (temporarily). My use case is battery power applications, where I have WLED running on a power bank. I don't need wifi to be on the entire time, but having it on for a little while when I plug it in is good. I found the "AP opens: Temporary (no connection after boot)" setting and thought that would be what I wanted, but it turns out to only disable broadcasting, not actually turning off the wifi hardware. I still leave it on because I don't need people connecting to my WLED controller and I can always unplug it if I need to change settings. Perhaps that could be another way that this change could be used, specifically helpful for battery-powered applications. If the setting actually saved power, that would be amazing!

It may also be wise to mention that it's only on for 5 minutes in the setting description somewhere, as it's not very clear what "no connection after boot" actually means in practice.

@rhkean
Copy link
Author

rhkean commented May 18, 2025

Hey folks. I just wanted to chime in here as a user who would love to turn off wifi (temporarily). My use case is battery power applications, where I have WLED running on a power bank. I don't need wifi to be on the entire time, but having it on for a little while when I plug it in is good. I found the "AP opens: Temporary (no connection after boot)" setting and thought that would be what I wanted, but it turns out to only disable broadcasting, not actually turning off the wifi hardware. I still leave it on because I don't need people connecting to my WLED controller and I can always unplug it if I need to change settings. Perhaps that could be another way that this change could be used, specifically helpful for battery-powered applications. If the setting actually saved power, that would be amazing!

It may also be wise to mention that it's only on for 5 minutes in the setting description somewhere, as it's not very clear what "no connection after boot" actually means in practice.

if you have a Button0 (Button connected to GPIO-0), try the Never setting. In code, it's actually called "Button only". It will not turn on the AP unless you hold the button for 5-6 seconds.

@blazoncek
Copy link
Collaborator

I noticed that the AP shutdown code was implemented 6 different times, so I consolidated it into a single WLED method.

You should've checked my fork as suggested.

It will not turn on the AP unless you hold the button for 5-6 seconds.

Beware, holding it too long and it will be factory reset.

@rhkean
Copy link
Author

rhkean commented May 19, 2025

I noticed that the AP shutdown code was implemented 6 different times, so I consolidated it into a single WLED method.

You should've checked my fork as suggested.

so... removed the shutdownAP method? close the PR?
I'm not sure what you would like for me to do at this point on the change?

@blazoncek
Copy link
Collaborator

Nothing, I'm just pointing out it clashes with what I've done some 9 months ago, yet I'm still not comfortable with my solution too.

@rhkean
Copy link
Author

rhkean commented May 20, 2025

Nothing, I'm just pointing out it clashes with what I've done some 9 months ago, yet I'm still not comfortable with my solution too.

ah... that makes sense. thanks

(side note: I've managed to get AP and BLE working together. STA and BLE still crashes, though... probably an advertising interval issue or something)

@xxv
Copy link
Contributor

xxv commented May 20, 2025

if you have a Button0 (Button connected to GPIO-0), try the Never setting. In code, it's actually called "Button only". It will not turn on the AP unless you hold the button for 5-6 seconds.

That's good to know and I'll try it out for devices that have a button. Some of mine don't, so I'll need to find another solution like fixing the temporary AP shutdown. I still think it is a good idea that should happen either in this or a follow-up PR once the shutdown code is consolidated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants